/**   
* @Title: StaffServiceImpl.java 
* @Package com.nebula.upms.service.impl 
* @Description: TODO(用一句话描述该文件做什么) 
* @author zhanghui   
* @date 2016年2月16日 下午2:12:14 
* @version V1.0   
*/
package com.nebula.upms.service.biz.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.nebula.common.datasource.mybatis.pager.DataPager;
import com.nebula.common.datasource.mybatis.pager.Pager;
import com.nebula.common.framework.BaseService;
import com.nebula.common.util.MD5Util;
import com.nebula.common.util.UUIDGenerator;
import com.nebula.upms.service.biz.StaffService;
import com.nebula.upms.service.model.po.*;
import com.nebula.upms.service.model.vo.StaffVO;
import com.nebula.upms.service.support.constant.UpmsLocalFinal;
import com.nebula.upms.service.support.datasource.UpmsDs;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.misc.UUDecoder;


/**
* @ClassName: StaffServiceImpl 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author zhanghui
* @date 2016年2月16日 下午2:12:14 
*  
*/
@Service("staffService")
@Transactional
public class StaffServiceImpl extends BaseService implements StaffService {

	/*
	 * <p>Title: getStaffVOListForPager</p>
	 * <p>Description: </p>
	 * @param params
	 * @param pager
	 * @return
	 * @see com.nebula.upms.service.StaffService#getStaffVOListForPager(java.util.Map, com.nebula.common.plugs.pager.Pager)
	 */
	@Override
	public DataPager<StaffVO> getStaffVOListForPager(Map<String, Object> params, Pager pager) {
		DataPager<StaffVO> dp= UpmsDs.instance().mybatis().ds().mapper("PrivilegeMapper").selectDataPager("selectStaffVOListForPage", params, pager);
		return dp;
	}

	/*
	 * <p>Title: genderList</p>
	 * <p>Description: </p>
	 * @return
	 * @see com.nebula.upms.service.StaffService#genderList()
	 */
	@Override
	public List<UpmsLookupCodePO> genderList() {
		UpmsLookupCodePOExample example1=new UpmsLookupCodePOExample();
		example1.createCriteria().andLookupTypeEqualTo(UpmsLocalFinal.PERSON_GENDER_Y6);
		List<UpmsLookupCodePO> ls=UpmsDs.instance().mybatis().ds().mapper().selectByExample(example1);
		return ls;
	}

	/*
	 * <p>Title: saveStaff</p>
	 * <p>Description: </p>
	 * @param params
	 * @see com.nebula.upms.service.StaffService#saveStaff(java.util.Map)
	 */
	@Override
	public void saveStaff(Map<String, Object> params,String privilegeGroupIdArray[]) {
		String departmentId=params.get("departmentId").toString();
		
		String addModel=params.get("addModel").toString();
		if(addModel.equals("ADD")){
			String name=params.get("name").toString();
			String gender=params.get("gender").toString();
			
			String birthdayStr=null;
			Long birthday=null;
			if(null!=params.get("birthdayStr")){
				birthdayStr=params.get("birthdayStr").toString();
				DateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
				try {
					birthday=format.parse(birthdayStr).getTime();
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
			
			String loginName=params.get("loginName").toString();
			String loginPassword=params.get("loginPassword").toString();
			
			String email=null;
			if(null!=params.get("email")){
				email=params.get("email").toString();
			}
			
			String qq=null;
			if(null!=params.get("qq")){
				qq=params.get("qq").toString();
			}
			
			String mobile=null;
			if(null!=params.get("mobile")){
				mobile=params.get("mobile").toString();
			}
			
			String telephone=null;
			if(null!=params.get("telephone")){
				telephone=params.get("telephone").toString();
			}
			
			String ext=null;
			if(null!=params.get("ext")){
				ext=params.get("ext").toString();
			}
			UpmsPersonPO UpmsPersonPO=new UpmsPersonPO();
			UpmsPersonPO.setPersonId(UUIDGenerator.random32UUID());
			UpmsPersonPO.setDepartmentId(departmentId);
			UpmsPersonPO.setLoginName(loginName);
			UpmsPersonPO.setLoginPassword(MD5Util.MD5(loginPassword));
			UpmsPersonPO.setType(UpmsLocalFinal.PERSON_TYPE_Y51);
			UpmsPersonPO.setName(name);
			UpmsPersonPO.setGender(gender);
			UpmsPersonPO.setEmail(email);
			UpmsPersonPO.setQq(qq);
			UpmsPersonPO.setMobile(mobile);
			UpmsPersonPO.setTelephone(telephone);
			UpmsPersonPO.setExt(ext);
			UpmsPersonPO.setBirthday(birthday);
			UpmsPersonPO.setCreateTime(new Date().getTime());
			UpmsPersonPO.setIsDelete(false);
			UpmsDs.instance().mybatis().ds().mapper().insert(UpmsPersonPO);
			
			
			UpmsStaffPO UpmsStaffPO=new UpmsStaffPO();
			UpmsStaffPO.setStaffId(UUIDGenerator.random32UUID());
			UpmsStaffPO.setDepartmentId(departmentId);
			UpmsStaffPO.setPersonId(UpmsPersonPO.getPersonId());
			UpmsStaffPO.setName(name);
			UpmsStaffPO.setIsDefault(true);
			UpmsStaffPO.setIsDelete(false);
			UpmsStaffPO.setCreateTime(new Date().getTime());
			UpmsDs.instance().mybatis().ds().mapper().insert(UpmsStaffPO);
			
			for (String privilegeGroupId : privilegeGroupIdArray) {
				UpmsPrivilegeGroupStaffPOKey UpmsPrivilegeGroupStaffPOKey=new UpmsPrivilegeGroupStaffPOKey();
				UpmsPrivilegeGroupStaffPOKey.setPrivilegeGroupId(privilegeGroupId);
				UpmsPrivilegeGroupStaffPOKey.setStaffId(UpmsStaffPO.getStaffId());
				UpmsDs.instance().mybatis().ds().mapper().insert(UpmsPrivilegeGroupStaffPOKey);
			}
		}else{
			String personId = params.get("personId").toString();
			boolean isDefault = Boolean.parseBoolean( params.get("isDefault").toString());
			UpmsPersonPO UpmsPersonPO=UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsPersonPO.class, personId);
			if(isDefault){
				UpmsStaffPOExample example1=new UpmsStaffPOExample();
				example1.createCriteria().andPersonIdEqualTo(personId);
				List<UpmsStaffPO> list=UpmsDs.instance().mybatis().ds().mapper().selectByExample(example1);
				for (UpmsStaffPO UpmsStaffPO : list) {
					UpmsStaffPO.setIsDefault(false);
					UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(UpmsStaffPO);
				}
			}
			
			
			UpmsStaffPO UpmsStaffPO=new UpmsStaffPO();
			UpmsStaffPO.setStaffId(UUIDGenerator.random32UUID());
			UpmsStaffPO.setDepartmentId(departmentId);
			UpmsStaffPO.setPersonId(personId);
			UpmsStaffPO.setName(UpmsPersonPO.getName());
			UpmsStaffPO.setIsDefault(isDefault);
			UpmsStaffPO.setIsDelete(false);
			UpmsStaffPO.setCreateTime(new Date().getTime());
			UpmsDs.instance().mybatis().ds().mapper().insert(UpmsStaffPO);
			
			for (String privilegeGroupId : privilegeGroupIdArray) {
				UpmsPrivilegeGroupStaffPOKey UpmsPrivilegeGroupStaffPOKey=new UpmsPrivilegeGroupStaffPOKey();
				UpmsPrivilegeGroupStaffPOKey.setPrivilegeGroupId(privilegeGroupId);
				UpmsPrivilegeGroupStaffPOKey.setStaffId(UpmsStaffPO.getStaffId());
				UpmsDs.instance().mybatis().ds().mapper().insert(UpmsPrivilegeGroupStaffPOKey);
			}
			
		}
		
		
	}

	/*
	 * <p>Title: getUpmsStaffPOByKey</p>
	 * <p>Description: </p>
	 * @param staffId
	 * @return
	 * @see com.nebula.upms.service.StaffService#getUpmsStaffPOByKey(java.lang.String)
	 */
	@Override
	public UpmsStaffPO getUpmsStaffPOByKey(String staffId) {
		return UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsStaffPO.class, staffId);
	}

	/*
	 * <p>Title: updateStaff</p>
	 * <p>Description: </p>
	 * @param params
	 * @param privilegeGroupIdArray
	 * @see com.nebula.upms.service.StaffService#updateStaff(java.util.Map, java.lang.String[])
	 */
	@Override
	public void updateStaff(Map<String, Object> params, String[] privilegeGroupIdArray) {
			String departmentId=params.get("departmentId").toString();
			String personId=params.get("personId").toString();
			String staffId=params.get("staffId").toString();
			
			boolean isDefault=true;
			if(null!=params.get("isDefault")){
				isDefault = Boolean.parseBoolean( params.get("isDefault").toString());
			}
			
			
			String name=params.get("name").toString();
			String gender=params.get("gender").toString();
			
			String birthdayStr=null;
			Long birthday=null;
			if(null!=params.get("birthdayStr")){
				birthdayStr=params.get("birthdayStr").toString();
				DateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
				try {
					birthday=format.parse(birthdayStr).getTime();
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
			boolean isReset=Boolean.valueOf(params.get("isReset").toString());
			String loginPassword=null;
			if(isReset){
				loginPassword=params.get("loginPassword").toString();
			}
			
			String email=null;
			if(null!=params.get("email")){
				email=params.get("email").toString();
			}
			
			String qq=null;
			if(null!=params.get("qq")){
				qq=params.get("qq").toString();
			}
			
			String mobile=null;
			if(null!=params.get("mobile")){
				mobile=params.get("mobile").toString();
			}
			
			String telephone=null;
			if(null!=params.get("telephone")){
				telephone=params.get("telephone").toString();
			}
			String loginName=null;
			if(null!=params.get("loginName")){
				loginName=params.get("loginName").toString();
			}
			
			String ext=null;
			if(null!=params.get("ext")){
				ext=params.get("ext").toString();
			}
			UpmsPersonPO UpmsPersonPO=new UpmsPersonPO();
			UpmsPersonPO.setPersonId(personId);
			UpmsPersonPO.setDepartmentId(departmentId);
			if(isReset){
				UpmsPersonPO.setLoginPassword(MD5Util.MD5(loginPassword));
			}
			UpmsPersonPO.setName(name);
			UpmsPersonPO.setGender(gender);
			UpmsPersonPO.setEmail(email);
			UpmsPersonPO.setQq(qq);
			UpmsPersonPO.setLoginName(loginName);
			UpmsPersonPO.setMobile(mobile);
			UpmsPersonPO.setTelephone(telephone);
			UpmsPersonPO.setExt(ext);
			UpmsPersonPO.setBirthday(birthday);
			UpmsPersonPO.setUpdateTime(new Date().getTime());
			UpmsPersonPO.setIsDelete(false);
			UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(UpmsPersonPO);
			
			if(isDefault){
				UpmsStaffPOExample example1=new UpmsStaffPOExample();
				example1.createCriteria().andPersonIdEqualTo(personId);
				List<UpmsStaffPO> list=UpmsDs.instance().mybatis().ds().mapper().selectByExample(example1);
				for (UpmsStaffPO UpmsStaffPO : list) {
					UpmsStaffPO.setIsDefault(false);
					UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(UpmsStaffPO);
				}
			}
			
			
			UpmsStaffPO UpmsStaffPO=new UpmsStaffPO();
			UpmsStaffPO.setStaffId(staffId);
			UpmsStaffPO.setDepartmentId(departmentId);
			UpmsStaffPO.setPersonId(UpmsPersonPO.getPersonId());
			UpmsStaffPO.setName(name);
			UpmsStaffPO.setIsDefault(isDefault);
			UpmsStaffPO.setIsDelete(false);
			UpmsStaffPO.setCreateTime(new Date().getTime());
			UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(UpmsStaffPO);
			
			
			UpmsPrivilegeGroupStaffPOExample example1=new UpmsPrivilegeGroupStaffPOExample();
			example1.createCriteria().andStaffIdEqualTo(staffId);
			UpmsDs.instance().mybatis().ds().mapper().deleteByExample(example1);
			
			for (String privilegeGroupId : privilegeGroupIdArray) {
				UpmsPrivilegeGroupStaffPOKey UpmsPrivilegeGroupStaffPOKey=new UpmsPrivilegeGroupStaffPOKey();
				UpmsPrivilegeGroupStaffPOKey.setPrivilegeGroupId(privilegeGroupId);
				UpmsPrivilegeGroupStaffPOKey.setStaffId(UpmsStaffPO.getStaffId());
				UpmsDs.instance().mybatis().ds().mapper().insert(UpmsPrivilegeGroupStaffPOKey);
			}
		
		}

	/*
	 * <p>Title: logicRemoveStaff</p>
	 * <p>Description: </p>
	 * @param staffId
	 * @see com.nebula.upms.service.StaffService#logicRemoveStaff(java.lang.String)
	 */
	@Override
	public void logicRemoveStaff(String staffId) {
		UpmsStaffPO UpmsStaffPO=UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsStaffPO.class, staffId);
		UpmsStaffPO.setIsDelete(true);
		UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(UpmsStaffPO);
		
		if(UpmsStaffPO.getIsDefault()){
			UpmsStaffPOExample example1=new UpmsStaffPOExample();
			example1.createCriteria().andPersonIdEqualTo(UpmsStaffPO.getPersonId()).andIsDeleteEqualTo(false);
			List<UpmsStaffPO> list=UpmsDs.instance().mybatis().ds().mapper().selectByExample(example1);
			for (UpmsStaffPO po : list) {
				po.setIsDefault(false);
				UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(UpmsStaffPO);
			}
			
			example1.clear();
			example1.createCriteria().andPersonIdEqualTo(UpmsStaffPO.getPersonId()).andIsDeleteEqualTo(false);
			example1.setOrderByClause("CREATE_TIME desc");
			UpmsStaffPO first=UpmsDs.instance().mybatis().ds().mapper().selectFirstByExample(example1);
			if(null!=first){
				first.setIsDefault(false);
				UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(first);
			}
		}
	}
}
